﻿using System;

using Xamarin.Forms;

using UriTypeConverter = Xamarin.Forms.UriTypeConverter;

namespace Outcoder.UI.Xaml
{
	public abstract class AppBarItemBase : View, IAppBarItem
	{
		static AppBarItemBase()
		{
			TextProperty = BindableProperty.Create("Text", typeof(string), typeof(AppBarItemBase), string.Empty, BindingMode.TwoWay);
			IconUriProperty = BindableProperty.Create("IconUri", typeof(Uri), typeof(AppBarItemBase), null, BindingMode.TwoWay);
		}

		#region Text

		public static readonly BindableProperty TextProperty;

		public string Text
		{
			get
			{
				return (string)GetValue(TextProperty);
			}
			set
			{
				SetValue(TextProperty, value);
			}
		}

		#endregion

		#region IconUri

		public static BindableProperty IconUriProperty;

		[TypeConverter(typeof(UriTypeConverter))]
		public Uri IconUri
		{
			get
			{
				return (Uri)GetValue(IconUriProperty);
			}
			set
			{
				SetValue(IconUriProperty, value);
			}
		}

		#endregion

		#region Tap Event

		public event EventHandler Tap;

		protected virtual void OnTap(EventArgs e)
		{
			var tempEvent = Tap;
			if (tempEvent != null)
			{
				tempEvent(this, e);
			}
		}

		void IAppBarItem.PerformTap()
		{
			if (!IsEnabled)
			{
				return;
			}

			OnTap(EventArgs.Empty);
		}

		#endregion
	}
}